import { defineStore } from 'pinia'

export const useOrderStore = defineStore('order', {
  state: () => ({
    orders: [],
    currentOrder: null,
    loading: false
  }),
  
  getters: {
    allOrders: (state) => state.orders,
    pendingOrders: (state) => state.orders.filter(order => order.status === 'pending'),
    completedOrders: (state) => state.orders.filter(order => order.status === 'completed'),
    cancelledOrders: (state) => state.orders.filter(order => order.status === 'cancelled')
  },
  
  actions: {
    async createOrder(cartItems, specialRequests) {
      this.loading = true
      try {
        // 模拟创建订单API
        const order = {
          id: Date.now().toString(),
          orderNumber: `LOVE${Date.now()}`,
          items: [...cartItems],
          totalPoints: cartItems.reduce((sum, item) => sum + (item.points * item.quantity), 0),
          status: 'pending',
          specialRequests,
          createdAt: new Date().toISOString(),
          updatedAt: new Date().toISOString()
        }
        
        this.orders.unshift(order)
        this.loading = false
        return { success: true, data: order }
      } catch (error) {
        this.loading = false
        return { success: false, message: error.message }
      }
    },
    
    async cancelOrder(orderId) {
      try {
        const order = this.orders.find(o => o.id === orderId)
        if (order) {
          order.status = 'cancelled'
          order.updatedAt = new Date().toISOString()
        }
        return { success: true }
      } catch (error) {
        return { success: false, message: error.message }
      }
    },
    
    async completeOrder(orderId) {
      try {
        const order = this.orders.find(o => o.id === orderId)
        if (order) {
          order.status = 'completed'
          order.updatedAt = new Date().toISOString()
        }
        return { success: true }
      } catch (error) {
        return { success: false, message: error.message }
      }
    },
    
    async getOrders() {
      this.loading = true
      try {
        // 模拟获取订单列表
        if (this.orders.length === 0) {
          this.orders = [
            {
              id: '1',
              orderNumber: 'LOVE202405010001',
              items: [
                { id: 1, name: '甜蜜双人牛排', points: 50, quantity: 1, image: '/static/images/dish1.png' },
                { id: 2, name: '浪漫心形蛋糕', points: 40, quantity: 2, image: '/static/images/dish2.png' }
              ],
              totalPoints: 130,
              status: 'pending',
              specialRequests: '请在晚上7点送达',
              createdAt: '2024-05-01T18:30:00Z',
              updatedAt: '2024-05-01T18:30:00Z'
            },
            {
              id: '2',
              orderNumber: 'LOVE202404280002',
              items: [
                { id: 3, name: '情侣麻辣火锅', points: 60, quantity: 1, image: '/static/images/dish3.png' }
              ],
              totalPoints: 60,
              status: 'completed',
              specialRequests: '',
              createdAt: '2024-04-28T19:15:00Z',
              updatedAt: '2024-04-28T19:15:00Z'
            }
          ]
        }
        this.loading = false
        return { success: true, data: this.orders }
      } catch (error) {
        this.loading = false
        return { success: false, message: error.message }
      }
    },
    
    getOrderById(orderId) {
      return this.orders.find(order => order.id === orderId)
    }
  }
})